package com.gcsj.gcsjspringboot.shiro;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gcsj.gcsjspringboot.dao.SysMenuDao;
import com.gcsj.gcsjspringboot.dao.SysUserDao;
import com.gcsj.gcsjspringboot.dao.SysUserRoleDao;
import com.gcsj.gcsjspringboot.entity.SysRoleEntity;
import com.gcsj.gcsjspringboot.entity.SysUserEntity;
import com.gcsj.gcsjspringboot.service.SysUserRoleService;
import com.gcsj.gcsjspringboot.service.SysUserService;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authc.credential.CredentialsMatcher;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 认证
 *
 * @author
 */
@Component
public class UserRealm extends AuthorizingRealm {
    @Autowired
    private SysUserDao sysUserDao;
    @Autowired
    private SysMenuDao sysMenuDao;
    @Autowired
    SysUserService userService;
    @Autowired
    private SysUserRoleDao sysUserRoleDao;
    /**
     * 授权(验证权限时调用)
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        SysUserEntity user = (SysUserEntity) principals.getPrimaryPrincipal();
        Long userId = user.getUserId();
        SimpleAuthorizationInfo sao = new SimpleAuthorizationInfo();
        List<SysRoleEntity> rolesByUser = sysUserRoleDao.getRolesByUser(userId);
        rolesByUser.forEach(i->{
            sao.addRole(i.getRoleCode());
        });
        return sao;
    }

    /**
     * 认证(登录时调用)
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(
            AuthenticationToken authcToken) throws AuthenticationException {
        UsernamePasswordToken token = (UsernamePasswordToken) authcToken;
        SysUserEntity user = userService.getOne(new QueryWrapper<SysUserEntity>().eq("username", token.getUsername()));
        if (null != user) {
            user = sysUserDao.selectOne(new QueryWrapper<SysUserEntity>().eq("username", token.getUsername()));
        } else {
            throw new UnknownAccountException("账号或密码不正确");
        }
        if (user.getStatus() == 0) {
            throw new LockedAccountException("账号已被禁用,请联系管理员");
        }
        SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(user, user.getPassword(), ByteSource.Util.bytes(user.getSalt()), getName());
        return info;

    }

    @Override
    public void setCredentialsMatcher(CredentialsMatcher credentialsMatcher) {
        HashedCredentialsMatcher shaCredentialsMatcher = new HashedCredentialsMatcher();
        shaCredentialsMatcher.setHashAlgorithmName(ShiroUtils.hashAlgorithmName);
        shaCredentialsMatcher.setHashIterations(ShiroUtils.hashIterations);
        super.setCredentialsMatcher(shaCredentialsMatcher);
    }
}
